<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;

/**
 * 团购管理-商品sku
 * @package app\erp\model
 */
class ErpGroupSponsorSku extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_group_sponsor_sku';

    /**
     * 查询数据
     * @param id int 团购商品ID
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function getInfoById($id, $cache = true){
        $info = $this -> cache($cache) -> find($id);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param group_sid 	            int     发起团购id
     * @param sku_id 		            int     单品id 参与的单品id
     * @param show_price 		        string  展示价格
     * @param limit_num 		        int     限购数量
     * @param sorts 	                int     排序
     * @param sp_status 		        int     0禁用 1启用
     * @param tries 		            int     购买次数限制
	 * @param sum_num 		            int     参与团购总库存数
	 * @param dj_num 		            int     冻结
	 * @param ksy_num 		            int     可使用
     * @param create_time 				int     创建时间
     * @param update_time 			    int     编辑时间
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function addData($group_sid, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $store_id){
        $data['group_sid']              = $group_sid;
        $data['sku_id']                 = $sku_id;
        $data['show_price']             = $show_price;
        $data['limit_num']              = $limit_num;
        $data['sorts']                  = $sorts;
        $data['sp_status']              = $sp_status;
        $data['tries']                  = $tries;
        $data['sum_num']                = $sum_num;
        $data['dj_num']                 = $dj_num;
        $data['ksy_num']                = $ksy_num;
        $data['store_id']               = $store_id;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	
	/**
     * 编辑数据
     * @param id 	                    int     团购商品ID
     * @param group_sid 	            int     发起团购id
     * @param sku_id 		            int     单品id 参与的单品id
     * @param show_price 		        string  展示价格
     * @param limit_num 		        int     限购数量
     * @param sorts 	                int     排序
     * @param sp_status 		        int     0禁用 1启用
     * @param tries 		            int     购买次数限制
     * @param sum_num 		            int     参与团购总库存数
     * @param create_time 				int     创建时间
     * @param update_time 			    int     编辑时间
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
	public function editData($id, $group_sid, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $store_id){
		$map = [];
		$map['id'] = $id;
		
		$data = [];
		$data['group_sid'] 		        = $group_sid;
        $data['sku_id'] 	            = $sku_id;
        $data['show_price'] 	        = $show_price;
        $data['limit_num'] 	            = $limit_num;
        $data['sorts'] 		            = $sorts;
		$data['sp_status']              = $sp_status;
        $data['tries']                  = $tries;
        $data['sum_num']                = $sum_num;
        $data['dj_num']                 = $dj_num;
        $data['ksy_num']                = $ksy_num;
        $data['store_id']               = $store_id;
        $res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 团购息 关联载入（一对一）
     */
    public function getErpGroupSponsor(){
        return $this -> hasOne('app\erp\model\ErpGroupSponsor','id', 'group_sid');
    }
}
